/**
 * 
 */
package com.EstateManager.Dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Estateschedual;
import com.EstateManager.Entity.Leaseorsaleinfo;
import com.EstateManager.Entity.User;

/**
 * 创建时间 : 下午8:59:08
 * @author Yonggang Liu
 *
 * EstateSchedualDao.java
 */

@Repository("estateSchedualDao")
public class EstateSchedualDao implements IModelBasicOperate<Estateschedual, Integer> {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	/* 预订状态 */
	public final static String SCHEDUAL_NOCHECK = "尚未审核";
	public final static String SCHEDUAL_SUCCESS = "预订成功";
	public final static String SCHEDUAL_FAIL= "预订失败";
	
	/**
	 * 
	 */
	public EstateSchedualDao() {
	}

	@Override
	public Estateschedual load(Integer id) throws HibernateException {
		return (Estateschedual)this.getCurrentSession().load(Estateschedual.class, id);
	}

	@Override
	public Estateschedual get(Integer id) throws HibernateException {
		return (Estateschedual)this.getCurrentSession().get(Estateschedual.class, id);
	}

	@Override
	public void persist(Estateschedual entity) throws HibernateException {
		this.getCurrentSession().persist(entity);
	}

	@Override
	public Integer save(Estateschedual entity) throws HibernateException {
		return (Integer)this.getCurrentSession().save(entity);
	}

	
	@Override
	public void saveOrUpdate(Estateschedual entity) throws HibernateException {
		this.getCurrentSession().saveOrUpdate(entity);
	}

	@Override
	public void detete(Estateschedual entity) throws HibernateException {
		this.getCurrentSession().delete(entity);
	}

	@Override
	public void flush() throws HibernateException {
		this.getCurrentSession().flush();
	}

	
	@Override
	public void update(Estateschedual entity) throws HibernateException {
		this.getCurrentSession().update(entity);
	}

	
	@Override
	public void merge(Estateschedual entity) throws HibernateException {
		this.getCurrentSession().merge(entity);
	}
	
	
	public Long getSechdualCount(Integer leseorsaleinfoId) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select count(estateschedual) "
				+ "from Estateschedual estateschedual "
				+ "where estateschedual.leaseorsaleinfo = :leaseorsaleinfo");
		Leaseorsaleinfo leaseorsaleinfo = new Leaseorsaleinfo();
		leaseorsaleinfo.setId(leseorsaleinfoId);
		query.setParameter("leaseorsaleinfo", leaseorsaleinfo);
		return (long)query.uniqueResult();
	}
	
	/**
	 * 根据用户id和租售信息id获取预订信息
	 * */
	public Estateschedual getEstateschedualByUserLease(int userid, int leaseorsaleinfoId) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select estateschedual "
				+ "from Estateschedual estateschedual "
				+ "where estateschedual.leaseorsaleinfo = :leaseorsaleinfo and estateschedual.user = :user");
		Leaseorsaleinfo leaseorsaleinfo = new Leaseorsaleinfo();
		leaseorsaleinfo.setId(leaseorsaleinfoId);
		User user = new User();
		user.setId(userid);
		query.setParameter("leaseorsaleinfo", leaseorsaleinfo);
		query.setParameter("user", user);
		return (Estateschedual)query.uniqueResult();
		
	}
	
	public List<Estateschedual> getEstateschedualsByOwnerIdAndResrult(int ownerId, String result, int pagezSize, int pageNum) {
		Query query = this.getCurrentSession().createQuery(""
				+ "select estateschedual "
				+ "from Estateschedual estateschedual "
				+ "where estateschedual.schedualresult = :schedualresult and estateschedual.leaseorsaleinfo in("
				+ "select leaseorsaleinfo "
				+ "from Leaseorsaleinfo leaseorsaleinfo "
				+ "where leaseorsaleinfo.estatebasicinfo in ("
				+ "select estatebasicinfo "
				+ "from Estatebasicinfo estatebasicinfo "
				+ "where estatebasicinfo.user = :user))");
		User user = new User();
		user.setId(ownerId);
		query.setParameter("schedualresult", result);
		query.setParameter("user", user);
		
		return (List<Estateschedual>)query.setMaxResults(pagezSize).setFirstResult((pageNum - 1) * pagezSize).list();
		 
	}

}
